import Layout from '@/views/Home/Layout'
import LoginView from '@/views/LoginView/LoginView.vue'
import Pool from '@/views/NewsPool/Pool'
import Published from '@/views/NewsPublished/Published'
import Release from '@/views/NewsRelease/Release'
import ReleaseDetail from '@/views/NewsRelease/ReleaseDetail.vue'
import Sistem from '@/views/NewsSistem/Sistem.vue'
import Preview from '@/views/Previewview/Preview.vue'
import PreviewDetail from '@/views/Previewview/PreviewDetail.vue'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      component: Layout,
      redirect: '/pool',
      children: [
        {
          path: 'pool',
          component: Pool,
        },
        {
          path: 'published',
          component: Published,
        },
        {
          path: 'release',
          component: Release,
        },
        {
          path: 'sistem',
          component: Sistem,
        },
        {
          path: '/log',
          component: () => import('@/views/LogView'),
        },
        {
          path: '/message',
          component: () => import('@/views/message'),
        },
        {
          path: '/I18',
          component:()=>import('@/views/I18n/I18')
        },
      ],
    },
    {
      path: '/login',
      component: LoginView,
    },
    {
      path: '/preview',
      component: Preview,
    },
    {
      path:'/previewdetail/:id',
      component:PreviewDetail
    },
    {
      path:'/releasedetail/:issueNo',
      component:ReleaseDetail
    }
  ],
})

router.beforeEach((to, from) => {
  const authenticated = () => {
    const loginInfo = localStorage.getItem('login')
    return !!loginInfo
  }
  if (to.path !== '/login' && !authenticated()) {
    return { path: '/login' }
  }
  if (to.path === '/login' && authenticated()) {
    return { path: '/pool' }
  }
  return true
})

export default router
